<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="X-UA-Compatible" content="IE=8" />
  <title>Advanced Python Transforms</title>
  <script src="/dlr/dlr.js" type="text/javascript"></script>
</head>
<body>
    
  <script type="text/python" class="page">
      from System import * # for Convert
      from System.Windows import Point, Visibility
      from System.Windows.Shapes import * # exposes Rectangle to scope since not added by default
      from System.Windows.Controls import Canvas
      from System.Windows.Media.Animation import * # exposes Storyboard
      from System.Windows.Media.Imaging import * # for bitmap
      from System.Windows.Media import * # for ImageBrush
      from System.Diagnostics import * # enables outputing to a debug window!
      from System.Windows.Interop import * # for Framerate
      from System.Net import * # for WebClient
      from System.Windows.Resources import * # for WebClient
      from Microsoft.Scripting.Silverlight.DynamicApplication import MakeUri
      
      layoutRoot = me.LayoutRoot
      tiles = me.tiles
      cover = me.cover
      
      mouse = Point(0, 0)
      _file = None
      webClient = WebClient()
      Tiles = None
      
      #
      # define class that procedurally creates Tile code instead of XAML
      #
      class Tile(Canvas): # inherits from Canvas
          def __init__(self):
          
              self.targetAngle = 0
              layoutRoot = Canvas()
              self.Children.Add(layoutRoot)
              
              self.bkgBrushPosition = TranslateTransform()
              self.bkgBrushPosition.X = 0
              self.bkgBrushPosition.Y = 0
              
              self.bkgBrush = ImageBrush()
              self.bkgBrush.AlignmentX = AlignmentX.Left
              self.bkgBrush.AlignmentY = AlignmentY.Top
              self.bkgBrush.Stretch = Stretch.None
              self.bkgBrush.Transform = self.bkgBrushPosition
              self.bkgBrush.ImageSource = BitmapImage(MakeUri("images/pic3.png"))
              
              self.bkg = Rectangle()
              self.bkg.Width = 30
              self.bkg.Height = 30
              self.bkg.Fill = self.bkgBrush
              self.bkg.RenderTransformOrigin = Point (0.45,0.45)
              layoutRoot.Children.Add(self.bkg)
      
              self.scale = ScaleTransform()
              self.scale.ScaleX = 1.0
              self.scale.ScaleY = 1.0
              
              self.rotation = RotateTransform()
              self.rotation.Angle = 0
             
              transformGroup = TransformGroup()
              transformGroup.Children.Add(self.scale)
              transformGroup.Children.Add(self.rotation)
              self.bkg.RenderTransform = transformGroup
                         
              def sb_Completed(sender, e):
                  self.rotation.Angle = self.rotation.Angle + ( self.targetAngle - self.rotation.Angle) * .13
                  sb.Begin()
              
              sb = Storyboard()
              sb.Completed += sb_Completed
              layoutRoot.Resources.Add("sb",sb)
              sb.Begin()
              
          def SetX(self, value):
              self.SetValue(Canvas.LeftProperty, Convert.ToDouble(value))
          def GetX(self):
              return self.GetValue(Canvas.LeftProperty)
          X = property(GetX, SetX)
          
          def SetY(self, value):
              self.SetValue(Canvas.TopProperty, Convert.ToDouble(value))
          def GetY(self):
              return self.GetValue(Canvas.TopProperty)
          Y = property(GetY, SetY)
          
      def Page_MouseMove(sender, e):
          global mouse
          global layoutRoot
          mouse = e.GetPosition(layoutRoot)
          
      def sb_Completed(sender, e):
          global tiles
          global sb
          global mouse
          for s in tiles.Children:
              _y = s.Y - mouse.Y
              _x = s.X - mouse.X
              distance = Math.Sqrt((_y * _y) + (_x * _x))
              rad = Math.Atan2(_y, _x)
              angle = rad * (180 / Math.PI)
              newScale = .3 + ((distance / 100.0));
              if newScale > 1.2:
                  newScale = 1.2
              s.scale.ScaleX = s.scale.ScaleY = newScale
              if distance < 100:
                  s.targetAngle = angle - 180
              else: 
                  s.targetAngle = 0
          sb.Begin()
      
      # TODO: Load up XAML instead of creating Tile Procedurally
      def webClient_OpenReadCompleted(sender, e):
          global _file
          if (e.Error != None):
              txtOut.Text = "Error: " + e.Error.Message
              return
          _file = StreamResourceInfo(e.Result, None)
          #Tile = XamlReader.Load("")
      
      layoutRoot.MouseMove += Page_MouseMove
      #webClient.OpenReadCompleted += webClient_OpenReadCompleted
      
      cover.Visibility = Visibility.Visible
      
      settings = Settings();
      settings.MaxFrameRate = 30;
      
      for x in range(0,10):
          for y in range(0, 10):
              s = Tile()
              s.X = 160 + (x * 25)
              s.Y = 25 + (y * 25)
              s.bkgBrushPosition.X = -s.X - 45;
              s.bkgBrushPosition.Y = -s.Y - 30;
              tiles.Children.Add(s)
      
      sb = Storyboard()
      sb.Completed += sb_Completed
      layoutRoot.Resources.Add("sb", sb)
      sb.Begin()
      
      #webClient.OpenReadAsync(MakeUri("Tile.xaml"))
  </script>

  <!-- 
    The following commented line of code is equivalent to the inline XAML
    and we recommend doing it this way. Only caveat, if you link to a XAML
    file, you'll be able to view the page correctly only through a server 
    (e.g. apache or IIS) because of security sandbox reasons. 
  -->
  <!--<script src="page.xaml" id="page" type="application/xml+xaml" width="600" height="300"></script>-->
  
  <script id="page" type="application/xml+xaml" width="600" height="300">
      <UserControl 
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:xaml="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
          Width="600" Height="300">
        <Canvas x:Name="LayoutRoot" Background="#222222">
          <Canvas x:Name="tiles"></Canvas>
          <Image x:Name="cover" Source="transforms/images/cover.png" Width="600" Height="330" Stretch="Fill" Visibility="Collapsed"></Image>
        </Canvas>
      </UserControl>
  </script>
  
</body>
</html>
